package com.hydra.baseui.recyclerlist;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.recyclerview.widget.RecyclerView;

import com.hydra.baseui.R;

/**
 * /**
 * 如果加了itemAnimator, 在item change时会闪一下，可以用下面两种方式避免
 * 1、getItemAnimator().setChangeDuration(0L);
 * 2、ItemAnimator animator = getItemAnimator();
 * if (animator instanceof SimpleItemAnimator) {
 *    ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
 * }
 */
public class HeaderListView extends RecyclerView {

    private ListViewLayoutManager mLayoutManager;

    public HeaderListView(Context context) {
        super(context);

        init(RecyclerView.VERTICAL);
    }

    public HeaderListView(Context context, int orientation) {
        super(context);

        init(orientation);
    }

    public HeaderListView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(attrs);
    }

    public HeaderListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if (attrs == null) {
            Log.e(getClass().getSimpleName(), "please set attrs !");
            return;
        }

        TypedArray array = getContext().obtainStyledAttributes(attrs,
                R.styleable.HeaderListView);

        int orientation = array.getInt(R.styleable.HeaderListView_android_orientation, RecyclerView.VERTICAL);

        array.recycle();

        init(orientation);
    }

    private void init(int orientation) {
        getRecycledViewPool().setMaxRecycledViews(0, 15);  //这个只是简单处理，因为会有其他的type

        setItemAnimator(null);

        mLayoutManager = new ListViewLayoutManager(getContext(), orientation, false);

        setLayoutManager(mLayoutManager);
    }

    @Override
    public void setAdapter(Adapter adapter) {
        super.setAdapter(buildWrappedAdapter(adapter));
    }

    protected RecyclerHeaderListAdapter buildWrappedAdapter(Adapter originAdapter) {
        if(originAdapter != null) {
            return new RecyclerHeaderListAdapter(originAdapter);
        }

        return null;
    }

    public void release() {
        setAdapter(null);
    }

    /**
     * after setAdapter
     */
    public void addHeaderView(View view) {
        Adapter adapter = getAdapter();

        if(adapter != null) {
            ((RecyclerHeaderListAdapter)adapter).addHeaderView(view);
        }
    }

    public void addHeaderView(int index, View view) {
        Adapter adapter = getAdapter();

        if(adapter != null) {
            ((RecyclerHeaderListAdapter)adapter).addHeaderView(index, view);
        }
    }

    /**
     * after setAdapter
     */
    public void removeHeaderView(View view) {
        Adapter adapter = getAdapter();
        if(adapter != null) {
            ((RecyclerHeaderListAdapter)adapter).removeHeaderView(view);
        }
    }

    /**
     * after setAdapter
     */
    public void addFooterView(View view) {
        Adapter adapter = getAdapter();

        if(adapter != null) {
            ((RecyclerHeaderListAdapter)adapter).addFooterView(view);
        }
    }

    /**
     * after setAdapter
     */
    public void addFooterView(int index, View view) {
        Adapter adapter = getAdapter();

        if(adapter != null) {
            ((RecyclerHeaderListAdapter)adapter).addFooterView(index, view);
        }
    }

    /**
     * after setAdapter
     */
    public void removeFooterView(View view) {
        Adapter adapter = getAdapter();
        if(adapter != null) {
            ((RecyclerHeaderListAdapter)adapter).removeFooterView(view);
        }
    }

    @Override
    public ListViewLayoutManager getLayoutManager() {
        return mLayoutManager;
    }
}
